package com.example.accessingdatajpa.service.impl;

import com.example.accessingdatajpa.exception.ResourceNotFoundException;
import com.example.accessingdatajpa.pojo.LiveBroadcast;
import com.example.accessingdatajpa.repository.LiveBroadcastRepository;
import com.example.accessingdatajpa.response.RestResponse;
import com.example.accessingdatajpa.service.ILiveBroadcastService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Service("liveBroadcastService")
public class LiveBroadcastService implements ILiveBroadcastService {
    @Autowired
    private LiveBroadcastRepository liveBroadcastRepository;

    //get all
    @Override
    @Transactional
    public RestResponse<List<LiveBroadcast>> queryAllLiveBroadcast(){
        RestResponse<List<LiveBroadcast>> response = new RestResponse<>();
        try {
            List<LiveBroadcast> data = liveBroadcastRepository.findAll();
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    //add new
    @Override
    @Transactional
    public RestResponse<String> addNewUp(LiveBroadcast liveBroadcast){
        RestResponse<String> response = new RestResponse<>();
        try {
            liveBroadcastRepository.save(liveBroadcast);
            response.setStatus(200);
            response.setMessage("ok");
            response.setData("save succeed");
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
            response.setData("save failed");
        }
        return response;
    }

    //update
    @Override
    @Transactional
    public RestResponse<String> updateUp(Long id,LiveBroadcast liveBroadcastDetails){
        RestResponse<String> response = new RestResponse<>();
        try {
            LiveBroadcast liveBroadcast = liveBroadcastRepository.findById(id)
                    .orElseThrow(() -> new ResourceNotFoundException("liveBroadcast","id",id));
            liveBroadcast.setUpName(liveBroadcastDetails.getUpName());
            liveBroadcast.setLiveDate(liveBroadcast.getLiveDate());
            liveBroadcast.setLivePicUrl(liveBroadcast.getLivePicUrl());
            liveBroadcast.setHeadPicUrl(liveBroadcastDetails.getHeadPicUrl());
            liveBroadcast.setLiveTitle(liveBroadcastDetails.getLiveTitle());
            //liveBroadcast.setPeopleNumber(liveBroadcastDetails.getPeopleNumber());
            response.setStatus(200);
            response.setMessage("ok");
            response.setData("update succeed");
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
            response.setData("update failed");
        }

        return response;
    }

    //delete
    @Override
    @Transactional
    public RestResponse<String> deleteUp(Long id){
        RestResponse<String> response = new RestResponse<>();
        try {
            LiveBroadcast liveBroadcast = liveBroadcastRepository.findById(id)
                    .orElseThrow(() -> new ResourceNotFoundException("liveBroadcast","id",id));
            liveBroadcastRepository.delete(liveBroadcast);
            response.setMessage("ok");
            response.setStatus(200);
            response.setData("delete success");
        }catch(RuntimeException e){
            response.setStatus(500);
            response.setMessage("ok");
            response.setData("delete failed");
        }

        return response;
    }
}
